//
//  CaptchaViewController.m
//  fxxiaoke
//
//  Created by 鞠超 on 16/9/10.
//  Copyright © 2016年 jc. All rights reserved.
//

#import "CaptchaViewController.h"

@interface CaptchaViewController () <UIScrollViewDelegate, UITextFieldDelegate>

@property (nonatomic, strong) UIScrollView * scrollView;

@property (nonatomic, strong) UILabel * lbl_tel;

@property (nonatomic, strong) UITextField * text_captcha;
@property (nonatomic, strong) UIButton * btn_obtain;
//倒计时
@property (nonatomic, assign) NSInteger time_number;
@property (nonatomic, strong) NSTimer * timer;

@end

@implementation CaptchaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void )p_navi
{
    _btnLeft.hidden = YES;
    _lblLeft.hidden = YES;
    _btnRight.hidden = YES;
    _lblRight.hidden = YES;
    
    _lblTitle.text = @"更换手机号";
    
    //左返回
    UILabel * lbl_return = [[UILabel alloc] initWithFrame:CGRectMake(11, 35, 40, 15)];
    lbl_return.text = @"取消";
    lbl_return.textColor = [UIColor whiteColor];
    lbl_return.font = [UIFont systemFontOfSize:17];
    [_topView addSubview:lbl_return];
    
    UIButton * btn_return = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_return.frame = CGRectMake(10, 22, 50, 40);
    [_topView addSubview:btn_return];
    [btn_return addTarget:self action:@selector(btn_returnAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

//左返回
- (void )btn_returnAction:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

//tabbar
- (void )viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
    
    [self.text_captcha becomeFirstResponder];
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    self.scrollView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.scrollView.contentSize = CGSizeMake(0, SCREEN_HEIGHT - 64);
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    
    UILabel * lbl_introduct = [[UILabel alloc] initWithFrame:CGRectMake(15, 40, SCREEN_WIDTH - 30, 20)];
    lbl_introduct.text = @"短信验证码已发送至";
    lbl_introduct.textColor = [UIColor blackColor];
    lbl_introduct.font = [UIFont systemFontOfSize:17.0f];
    lbl_introduct.textAlignment = NSTextAlignmentCenter;
    [self.scrollView addSubview:lbl_introduct];
    
    self.lbl_tel = [[UILabel alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(lbl_introduct.frame) + 10, SCREEN_WIDTH - 30, 20)];
    self.lbl_tel.text = [NSString stringWithFormat:@"+86 %@",self.str_Tel];
    self.lbl_tel.textColor = [UIColor blackColor];
    self.lbl_tel.font = [UIFont systemFontOfSize:17.0f];
    self.lbl_tel.textAlignment = NSTextAlignmentCenter;
    [self.scrollView addSubview:self.lbl_tel];
    
    //
    self.text_captcha = [[UITextField alloc] init];
    self.text_captcha.frame = CGRectMake(15, CGRectGetMaxY(self.lbl_tel.frame) + 60, SCREEN_WIDTH - 30, 25);
    if(SCREEN_HEIGHT == 480)
    {
        self.text_captcha.frame = CGRectMake(15, CGRectGetMaxY(self.lbl_tel.frame) + 40, SCREEN_WIDTH - 30, 25);
    }
    self.text_captcha.placeholder = @"请输入验证码";
    self.text_captcha.font = [UIFont systemFontOfSize:17.0f];
    self.text_captcha.delegate = self;
    self.text_captcha.keyboardType = UIKeyboardTypeNumberPad;
    self.text_captcha.textAlignment = NSTextAlignmentCenter;
    self.text_captcha.secureTextEntry = YES;
    
    [self.scrollView addSubview:self.text_captcha];
    [self.text_captcha addTarget:self action:@selector(textFielddidChanged) forControlEvents:UIControlEventEditingChanged];
    //
    self.btn_obtain = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.btn_obtain.frame = CGRectMake(SCREEN_WIDTH / 2 - 100, CGRectGetMaxY(self.text_captcha.frame) + 20, 200, 30);
    if(SCREEN_HEIGHT == 480)
    {
        self.btn_obtain.frame = CGRectMake(SCREEN_WIDTH / 2 - 100, CGRectGetMaxY(self.text_captcha.frame) + 10, 200, 30);
    }
    [self.btn_obtain setTitle:@"重新获取验证码" forState:(UIControlStateNormal)];
    [self.btn_obtain setTitleColor:[UIColor colorWithRed:45/255.0 green:150/255.0 blue:248/255.0 alpha:1.0f] forState:(UIControlStateNormal)];
    self.btn_obtain.titleLabel.font = [UIFont systemFontOfSize:17.0f];
    [self.scrollView addSubview:self.btn_obtain];
    [self.btn_obtain addTarget:self action:@selector(btn_obtainAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

#pragma mark - textField代理
- (void )textFielddidChanged
{
    if([self.text_captcha.text length] == 4)
    {
        NSLog(@"自动验证验证码是否正确");
    }
}

#pragma mark - 点击重新获取验证码
- (void )btn_obtainAction:(UIButton *)sender
{
    NSLog(@"点击重新获取验证码");
    
    self.btn_obtain.userInteractionEnabled = NO;

    self.time_number = 60;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    
    [self.btn_obtain setTitle:[NSString stringWithFormat:@"60秒后重新获取验证码"] forState:(UIControlStateNormal)];
    [self.btn_obtain setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
}

#pragma mark - 时间倒计时
- (void)timerAction
{
    self.time_number --;
    
    if(self.time_number <= 0)
    {
        [self.timer invalidate];
        
        self.btn_obtain.userInteractionEnabled = YES;
        [self.btn_obtain setTitle:@"重新获取验证码" forState:(UIControlStateNormal)];
        [self.btn_obtain setTitleColor:[UIColor colorWithRed:45/255.0 green:150/255.0 blue:248/255.0 alpha:1.0f] forState:(UIControlStateNormal)];
    }
    else
    {
        [self.btn_obtain setTitle:[NSString stringWithFormat:@"%ld秒后重新获取验证码",self.time_number] forState:(UIControlStateNormal)];
        [self.btn_obtain setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    }
}
















@end
